package bmCheckClassId.zhaosheng;

import com.rslai.commons.util.StringUtil;
import com.rslai.commons.util.excel.ExcelRead;
import com.rslai.commons.util.excel.ExcelWrite;
import com.rslai.commons.util.excel.Head;
import com.rslai.commons.util.excel.exception.ExcelException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;

/**
 * Created by rslai on 2018/12/11.
 */
public class zhaosheng {

    /**
     * 遍历cityUrlList.xls，在每个城市页内遍历课程
     * @throws Exception
     */
    public void ergodicCityUrlList() throws Exception {
        File file = new File("./output/cityUrlList.xls");
        FileInputStream fis = new FileInputStream(file);
        ExcelRead excelReader = new ExcelRead(fis);


        while (excelReader.next()) {
            if (!"http://v.huatu.com/".equals(excelReader.getString("城市 URL"))) {
                ExcelWrite excelWrite = this.createExcel();
                String cityName_tmp = excelReader.getString("课程分类") + "_" + excelReader.getString("课程类别") + "_" + excelReader.getString("城市");
                cityClass(excelWrite, cityName_tmp, excelReader.getString("城市 URL"), excelReader.getString("城市 URL"));

                // 如果没有任何课程写入一下地址
                if (excelWrite.getTotalRow() == 1) {
                    excelWrite.addBody(new String[]{cityName_tmp, excelReader.getString("城市 URL")});
                }

                // 写入excel文件
                FileOutputStream fos = new FileOutputStream("./output/" + cityName_tmp.replaceAll("\\?","") + ".xls");
                excelWrite.write(fos);
                fos.close();
            }
        }

//        ExcelWrite excelWrite = this.createExcel();
//        cityClass(excelWrite, "医疗卫生考试_面试课程_甘肃", "http://bm.huatu.com/zhaosheng/ah/itbs.html", "http://bm.huatu.com/zhaosheng/zj/gwyms.html");
//        // 分类爬完后关闭excel文件
//        FileOutputStream fos = new FileOutputStream("./output/cityUrlList123.xls");
//        excelWrite.write(fos);
//        fos.close();
    }

    public boolean cityClass(ExcelWrite excelWrite, String cityName, String parentUrl, String cityURL) {
        System.out.println("在 “" + cityName + "” 中查找课程 url：" + parentUrl);
        Document doc = null; // 加载城市课程页
        try {
            doc = Jsoup.connect(cityURL).get();

            // 拿到一个页面遍历里边是否有table
            Elements tables = doc.select("table");
            for (Element table : doc.select("table")) {
                table(excelWrite, cityName, parentUrl, table); // 遍历一个table将内容写入excel文件
            }

            if (doc!=null && !doc.text().trim().equals("")) {
                Elements elements = doc.select("iframe");
                for (Element e : elements) {
                   if (StringUtil.isNotBlank(e.attr("src").trim())) {
                        cityClass(excelWrite, cityName, parentUrl + " -> " + e.attr("src").trim(), e.attr("src").trim());
                   }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 遍历一个table将内容写入excel文件
     * @param excelWrite
     * @param cityStr 城市文字
     * @param parentUrl 父URL地址
     * @param table table对象
     * @return
     */
    private boolean table(ExcelWrite excelWrite, String cityStr, String parentUrl, Element table) {
        Elements trs = table.select("tr");
        Elements head = null;
        int line = 0;
        if (trs.size() > 1) {
            // 为了兼容有些table首行显示表格题目
            for (Element tre : trs) {
                if (tre.select("td,th").size() > 1) {
                    head = tre.select("td,th");
                    line++;
                    break;
                }
                line++;
            }
        }

        for (int i= line; i<trs.size(); i++) {
            if (trs.get(i).select("td").size() >= 2) {
                ArrayList<String> lineList = new ArrayList<String>();
                lineList.add(cityStr);
                lineList.add(parentUrl);

                int j = 0;
                for (Element e : trs.get(i).select("td")) {
                    String str = "";

                    // 如果 table 有头吧头加上
                    if (null != head) {
                        if (j < head.size()) {
                            str = str + head.get(j).text() + " : ";
                        }
                    }

                    str = str + e.text(); // 表格上的内容
                    Elements link = e.select("a");

                    if (link.size() >= 1) {
                        str = str + " url: " + link.get(0).attr("href");
                    }
                    lineList.add(str);
                    j++;
                }
                excelWrite.addBody(lineList);
            }
        }
        return true;
    }

    /**
     * 创建课程列表excel
     * @return excel对象
     * @throws com.rslai.commons.util.excel.exception.ExcelException
     */
    private ExcelWrite createExcel() throws ExcelException {
        ExcelWrite excelWrite = new ExcelWrite();
        Head head;
        head = new Head(0, "课程类别", 25, null);
        excelWrite.addHead(head);
        head = new Head(1, "URL", 20, null);
        excelWrite.addHead(head);
        head = new Head(2, "td1", 25, null);
        excelWrite.addHead(head);
        head = new Head(3, "td2", 40, null);
        excelWrite.addHead(head);
        head = new Head(4, "td3", 30, null);
        excelWrite.addHead(head);
        head = new Head(5, "td4", 30, null);
        excelWrite.addHead(head);
        head = new Head(6, "td5", 30, null);
        excelWrite.addHead(head);
        head = new Head(7, "td6", 30, null);
        excelWrite.addHead(head);
        head = new Head(8, "td7", 30, null);
        excelWrite.addHead(head);
        head = new Head(9, "td8", 30, null);
        excelWrite.addHead(head);
        head = new Head(10, "td9", 30, null);
        excelWrite.addHead(head);
        return excelWrite;
    }
}
